package com.campus.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.campus.domain.dto.UserDTO;
import com.campus.domain.entity.User;
import com.campus.service.IUserService;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UserDetailServiceImpl implements UserDetailsService {
    @Resource
    @Lazy
    private IUserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.selectByUserName(username);
        if(user == null){
            throw new RuntimeException("用户名或密码错误");//当输错信息后会提示这个错误信息
        }
        //查询用户按钮权限
        List<String> buttonPermission = userService.getButtonPermission(user.getId());
        UserDTO userDTO = BeanUtil.copyProperties(user, UserDTO.class);
        userDTO.setPermissions(buttonPermission);
        return userDTO;
    }
}
